package utils

import "server/vars"

var CACHE_PREFIX = "goshop:"

func SetString(key string, data string) error {
	Rds.Set(CACHE_PREFIX+key, data)
	return nil
}

func SetStringEx(key string, data string) error {
	Rds.Setex(CACHE_PREFIX+key, data, vars.CacheTime)
	return nil
}

func SetCache(key string, data any) error {
	dataStr := StructToJson(data)
	err := Rds.Set(CACHE_PREFIX+key, dataStr)
	if err != nil {
		return err
	}
	return nil
}

func SetCacheEx(key string, data any) error {
	dataStr := StructToJson(data)
	err := Rds.Setex(CACHE_PREFIX+key, dataStr, vars.CacheTime)
	if err != nil {
		return err
	}
	return nil
}

func GetString(key string) string {
	data, err := Rds.Get(CACHE_PREFIX + key)
	if err != nil {
		return ""
	}
	return data
}

func GetCache(key string, data any) error {
	dataStr, err := Rds.Get(CACHE_PREFIX + key)
	if err != nil {
		return err
	}
	err = JsonToStruct(dataStr, &data)
	if err != nil {
		return err
	}
	return nil
}

func DelCache(key string) error {
	_, err := Rds.Del(CACHE_PREFIX + key)
	if err != nil {
		return err
	}
	return nil
}
